package com.kitty.web.common;


import cn.hutool.core.io.file.FileNameUtil;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 文件上传工具类
 * 
 * @author soul
 */
public class FileUploadUtils
{
    /**
     * 默认大小 50M
     */
    public static final long DEFAULT_MAX_SIZE = 500 * 1024 * 1024;

    /**
     * 默认的文件名最大长度 100
     */
    public static final int DEFAULT_FILE_NAME_LENGTH = 100;

    /**
     * 默认上传的地址
     */
    public static String defaultBaseDir =  "C:/phpstudy_pro/WWW";

    private static int counter = 0;

    public static void setDefaultBaseDir(String defaultBaseDir)
    {
        FileUploadUtils.defaultBaseDir = defaultBaseDir;
    }

    public static String getDefaultBaseDir()
    {
        return defaultBaseDir;
    }



    public static final String[] DEFAULT_ALLOWED_EXTENSION2 = {
            "zip" };
    /**
     * 根据文件路径上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @return 文件名称
     * @throws IOException
     */
    public static final String upload2(String baseDir, MultipartFile file) throws IOException
    {
        try
        {
            return upload2(baseDir, file, DEFAULT_ALLOWED_EXTENSION2);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

    public static final String upload2(String baseDir, MultipartFile file, String[] allowedExtension)
            throws Exception
    {
        int fileNamelength = file.getOriginalFilename().length();
        if (fileNamelength >200)
        {
            throw new RuntimeException("200");
        }

        String fileName = extractFilename2(file);
        String name = FileNameUtil.extName(fileName);
        String pathFileName = "";
        if(name.contains("zip")){
            File desc = getAbsoluteFile2(baseDir, fileName);
            file.transferTo(desc);
            pathFileName = getPathFileName(baseDir, fileName);
        }
        return pathFileName;
    }
    /**
     * 编码文件名
     */
    public static final String extractFilename2(MultipartFile file)
    {
        String fileName = file.getOriginalFilename();
        String filePath = FileUploadUtils.defaultBaseDir;
        fileName =  filePath+"/" + fileName;
        return fileName;
    }


    private static final File getAbsoluteFile2(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(fileName);

        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        if (!desc.exists())
        {
            desc.createNewFile();
        }
        return desc;
    }

    private static final String getPathFileName(String uploadDir, String fileName) throws IOException
    {
        int dirLastIndex =FileUploadUtils.defaultBaseDir.length() + 1;
        String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
        String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
        return pathFileName;
    }

    /**
     * 判断MIME类型是否是允许的MIME类型
     *
     * @param extension
     * @param allowedExtension
     * @return
     */
    public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
    {
        for (String str : allowedExtension)
        {
            if (str.equalsIgnoreCase(extension))
            {
                return true;
            }
        }
        return false;
    }



}